<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\Request;
use think\facade\View;

// 投诉管理
class Report extends BaseController
{
    public function index()
    {
        //获取帖子话题类型
        $data = Db::name('topic_type')
            ->where('status', '=', 1)
            ->field('id, name')
            ->select();
        View::assign('data', $data);
        return View::fetch();
    }


    //+数据加载
    public function load(Request $request)
    {
        if(!check_jurisdiction(session('admin_auth.role_id'), $this->control, $this->action)) {
            return json(['code' => 500, 'data' => '', 'msg' => '无操作权限']);
        }

        $get = $request->get();

        $where = [];

        if (isset($get['type'])) {
            if (!empty($get['type'])) {
                $where[] = ['','EXP',Db::raw('FIND_IN_SET("'.$get['type'].'",type2)')];
            }
        }

        if (isset($get['status'])) {
            if ($get['status'] != '') {
                $where[] = ['status', '=', $get['status']];
            } else {
                $where[] = ['status', 'in', '0,1'];
            }
        } else {
            $where[] = ['status', 'in', '0,1'];
        }

        if (isset($get['time'])) {
            if (!empty($get['time'])) {
                $t = explode(' - ',$get['time']);
                $where[] = ['add_time', 'between', [strtotime($t[0]),strtotime($t[1])+86399]];
            }
        }

        if (isset($get['keys'])) {
            if (!empty($get['keys'])) {
                $where[] = ['content|name1|name2', 'like', '%'.$get['keys'].'%'];
            }
        }

        $total = Db::name('report')
            ->where($where)
            ->count();

        $rows = Db::name('report')
            ->where($where)
            ->order('id', 'desc')
            ->page($get['page'], $get['limit'])
            ->select();
        $data = array(
            'code'=> 0,
            'msg'=> '',
            'count' => $total,
            'data'=> $rows
        );
        return json($data);
    }



    //+忽略
    public function ignore(Request $request)
    {
        if(!check_jurisdiction(session('admin_auth.role_id'), $this->control, $this->action)) {
            return $this->outputAjax('无操作权限');
        }

        $id = $request->post('id');

        if (empty($id)) {
            return $this->outputAjax('参数不足！');
        }
        Db::startTrans();
        try {

            $ids = explode(',',$id);

            foreach ($ids as $key => $value) {
                $t = Db::table('o_report')->find($value);
                if ($t['status'] != 0) {
                    Db::rollback();
                    return $this->outputAjax('ID为'.$t['id'].'的数据已操作');
                }
                if (!Db::table('o_report')->where('id', '=', $value)->update(['status' => 2])) {
                    Db::rollback();
                    return $this->outputAjax('操作失败！');
                }
            }
            Db::commit();
            return $this->outputAjax('操作成功！',0);

        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->outputAjax($e->getMessage());
        }
    }


    //+处罚
    public function punish(Request $request)
    {
        if(!check_jurisdiction(session('admin_auth.role_id'), $this->control, $this->action)) {
            return $this->outputAjax('无操作权限');
        }

        $id = $request->post('id');

        if (empty($id)) {
            return $this->outputAjax('参数不足！');
        }
        Db::startTrans();
        try {

            $ids = explode(',',$id);

            foreach ($ids as $key => $value) {
                $t = Db::table('o_report')->find($value);
                if ($t['status'] != 0) {
                    Db::rollback();
                    return $this->outputAjax('ID为'.$t['id'].'的数据已操作');
                }

                if (!Db::table('o_report')->where(['id'=>$value])->update(['status' => 1])) {
                    Db::rollback();
                    return $this->outputAjax('操作失败[1]');
                }

                if (!Db::table('o_topic')->where('id', '=', $t['aid'])->update(['status' => 3])) {
                    Db::rollback();
                    return $this->outputAjax('操作失败[2]');
                }
            }

            Db::commit();
            return $this->outputAjax('操作成功！',0);

        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->outputAjax($e->getMessage());
        }
    }
}